package edu.xhu;


import com.net.LoadData;

import edu.xhu.view.MyTurnplateView;
import edu.xhu.view.MyTurnplateView.OnTurnplateListener;
import edu.xhu.view.MyTurnplateView.Point;
import edu.xhu.util.DatabaseAdapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class XiHuaActivity extends Activity implements OnTurnplateListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		int height = getWindowManager().getDefaultDisplay().getHeight();
		int width = getWindowManager().getDefaultDisplay().getWidth();
		getWindow().setBackgroundDrawableResource(R.drawable.hot1);
		MyTurnplateView myView = new MyTurnplateView(this, width/2, height/2, width/3+30);
		myView.setOnTurnplateListener(this);
		setContentView(myView);
	}

	@Override
	public void onPointTouch(Point point) {
		// TODO Auto-generated method stub
	    int flag = point.flag;
		Intent intent = new Intent();
		switch (flag)
		{
		case 0:
			intent.setClass(this, EmploymentActivity.class);//就业信息
			intent.putExtra("flag",0);
			this.startActivity(intent);
			break;
		case 1:
			intent.setClass(this, FeedbackActivity.class);//反馈信息
			intent.putExtra("flag",1);
			this.startActivity(intent);
			break;
		case 2:
			intent.setClass(this, Library.class);//图书馆
			intent.putExtra("flag",2);
			this.startActivity(intent);
			break;
		case 3:
			intent.setClass(this, LoginActivity.class);//教务系统
			intent.putExtra("flag",3);
			this.startActivity(intent);
			break;
		case 4:
			intent.setClass(this, LoginActivity.class);//检查更新
			intent.putExtra("flag",4);
			this.startActivity(intent);
			break;
		/*case 5:
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("flag",5);
			this.startActivity(intent);
			break;
		case 6:
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("flag",6);
			this.startActivity(intent);
			break;
		case 7:
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("flag",7);
			this.startActivity(intent);
			break;
		case 8:
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("flag",8);
			this.startActivity(intent);
			break;
		case 9:
			intent.setClass(this, LoginActivity.class);
			intent.putExtra("flag",9);
			this.startActivity(intent);
			break;*/

		default:
			break;
		}
	}
	
	private void showQuitDialog()
	{
		Dialog dlg = new AlertDialog.Builder(XiHuaActivity.this)
		.setTitle("提示")
		.setMessage("是否退出程序")
		.setNeutralButton("是", new DialogInterface.OnClickListener() {
			
			public void onClick(DialogInterface dialog, int which) {
				LoadData.clear();
		    	finish();
			}
		})
		.setNegativeButton("否", new DialogInterface.OnClickListener() 	{
			
			public void onClick(DialogInterface dialog, int which) {
				
			}
		}).create();
		dlg.show();
	}
	
	
	private void showHintDialog(){
    	Builder builder = new Builder(this);
    	builder.setTitle("提示")
    	.setMessage("是否清除本地所有的数据？")
    	.setPositiveButton("清除", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
						// 清除缓存
						DatabaseAdapter dbAdapter = new DatabaseAdapter(
								XiHuaActivity.this);
						dbAdapter.openDatabase();
						dbAdapter.deleteAll();
						dbAdapter.closeDatabase();
						
						SharedPreferences spPreferences = getSharedPreferences("update", MODE_WORLD_WRITEABLE);
						Editor editor = spPreferences.edit();
						editor.putBoolean("update", false);
						editor.putString("version", "");
						editor.putLong("time", 0);
						editor.commit();
						
						Toast.makeText(XiHuaActivity.this, "清除结束", 0).show();
			}
		}).setNegativeButton("不清除", null).create().show();
    }
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId())
		{
		case R.menu.clear:
			//清除缓存
			showHintDialog();
			break;
			
		case R.menu.about:
			//关于我们
			Intent intent = new Intent(XiHuaActivity.this, AboutActivity.class);
			startActivityForResult(intent, 0);
			overridePendingTransition(R.anim.zoom_enter,
					R.anim.zoom_exit);//进入和退出时的效果
			break;
			
		case R.menu.help:
			//帮助
			Intent intent1 = new Intent(XiHuaActivity.this, Help.class);
			startActivity(intent1);
			break;
			
		case R.menu.exit:
			//退出系统
			showQuitDialog();
			break;
		}
		
		return true;
	}

}
